//
//  JWStudentResutCtry.m
//  test
//
//  Created by 李彦宏 on 2018/1/30.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "JWStudentResutCtry.h"
#import "JWSearcell.h"//搜索结果cell
#import "JWStudentOptionsVC.h"//学员详情
#import "JWUserManager.h"
#import "SDUserModel.h"
@interface JWStudentResutCtry ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) SDUserModel *SDmodel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *SDarrylist;//学生列表
@end

@implementation JWStudentResutCtry

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置CGRectZero从导航栏下开始计算
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }

    [self.view addSubview:self.tableView];

    
    self.SDmodel=[JWUserManager SDme];
    
    
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(handleColorChange:) name:@"searchBarDidChange" object:nil];
    // Do any additional setup after loading the view.
}
-(void)handleColorChange:(NSNotification* )sender
{
    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         [EasyShowLodingView hidenLoding];
    });
      
    
    NSString *text = sender.userInfo[@"searchText"];
   //   NSLog(@"%@", text);
    NSDictionary *dic=@{
                        @"jwid":JWid,
                        @"jwpassword":JWpassword,
                        @"searchname":text,
                        };
    [[AFNetworkTool shareManager]postJSONWithUrl: JWSDsearch_list parmas:dic successData:^(id json) {
        //研究生数据
        [self.SDarrylist removeAllObjects];
        __weak typeof(self) weakslf = self;
        NSArray *GRarry=json[@"studentlist"];
        for (int i=0; i<GRarry.count; i++){
            self.SDmodel=[SDUserModel yy_modelWithJSON:GRarry[i]];
            [JWUserManager saveSD:weakslf.SDmodel];
            [weakslf.SDarrylist addObject:weakslf.SDmodel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.tableView reloadData];
            
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    } ];
    
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
      return self.SDarrylist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JWSearcell *cell=[JWSearcell cellwithTableview:tableView];
    self.SDmodel=self.SDarrylist[indexPath.row];
    
    
    cell.nameLab.text = self.SDmodel.name;
    cell.specialLab.text=self.SDmodel.college;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.SDmodel=self.SDarrylist[indexPath.section];
    [JWUserManager saveSD:self.SDmodel];
    self.didSelectText([tableView cellForRowAtIndexPath:indexPath].textLabel.text);
    
    JWStudentOptionsVC *vc=[[JWStudentOptionsVC alloc]init];
    vc.title=[NSString stringWithFormat:@"%@的详情",self.SDmodel.name];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  80;
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *Foot=[[UIView alloc]init];
    Foot.backgroundColor=[UIColor whiteColor];
    
    return Foot;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *head=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, CGFLOAT_MIN)];
    
    return head;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.didSelectText(@"");
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)SDarrylist{
    if(!_SDarrylist){
        _SDarrylist=[[NSMutableArray alloc]init];
    }
    return _SDarrylist;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.rowHeight=50;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}


@end
